package com.itheima.app.manager;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.Notification;
import com.itheima.domain.db.Question;
import com.itheima.domain.db.User;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.SettingVo;
import com.itheima.service.db.*;
import com.itheima.util.ConstantUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

@Component
public class SettingsManager {

    @Reference
    private NotificationService notificationService;//通知的service
    @Reference
    private QuestionService questionService;//问题的service

    //查询设置信息
    public SettingVo findMySettings() {
        //1.从th中获用户信息
        User user = UserHolder.get();
        //2.根据userId查询陌生人信息
        Question question = questionService.findByUserId(user.getId());
        //3.根据userId查询推送设置
        Notification notification = notificationService.findByUserId(user.getId());
        //4.组装返回结果
        SettingVo settingVo = new SettingVo();
        //4-1设置用户信息
        settingVo.setId(user.getId());
        settingVo.setPhone(user.getPhone());
        //4-2设置陌生人问题
        if (question == null) {
            settingVo.setStrangerQuestion("你喜欢星星还是月亮？");
        } else {
            settingVo.setStrangerQuestion(question.getStrangerQuestion());
        }
        //4-3设置推送
        if (notification != null) {
            BeanUtil.copyProperties(notification, settingVo);
        }
        return settingVo;
    }

    //保存或更新用户问题
    public void saveOrUpdateQuestions(String content) {
        //获得对象
        //1.先查看用户有没有这个问题
        Long id = UserHolder.get().getId();
        Question question = questionService.findByUserId(id);
        if (question == null) {
            //3.如果没有添加
            question = new Question();//创建对象
            question.setStrangerQuestion(content);//设置问题
            question.setUserId(id);
            questionService.save(question);
        } else {
            //2.如果有修改
            question.setStrangerQuestion(content);
            questionService.update(question);
        }
    }
    //保存或者更新推送设置
    public void saveOrUpdateNotifications(Notification notification) {
        //这个id是userId
        Long userId = UserHolder.get().getId();
        //1.先查看用户有没有这个通知
        Notification tempNotification = notificationService.findByUserId(userId);
        if (tempNotification == null) {
            //3.如果没有添加
            notification.setUserId(userId);//赋值用户的id数据
            notificationService.save(notification);
        } else {
            notification.setId(tempNotification.getId());
            //2.如果有 只修改三个通知字段
            notificationService.update(notification);

            //notificationService.update(浏览器传入的对象 缺少id);
            //notificationService.update(数据库查询的对象 三个通知);
        }


    }
    @Reference
    private BlackListService blackListService;
    /**
     * 黑名单用户查询
     * @param pageNumber 页码
     * @param pageSize   每页展示的个数
     * @return
     */
    public PageBeanVo findBlackList(Integer pageNumber, Integer pageSize) {
        //1.获得用户的id
        Long userId = UserHolder.get().getId();
        //2.调用分页进行查询
        return blackListService.findBlackUserByUserId(pageNumber, pageSize, userId);
    }

    /**
     *  删除黑名单用户
     * @param blackUserId   黑名单用户的id
     * @return
     */
    public Map deleteBlackUser(Long blackUserId) {
        //获得当前用户的id
        Long userId = UserHolder.get().getId();
        //删除用户
        blackListService.deleteBlackUser(userId, blackUserId);
        return null;
    }
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //修改手机号时    发送短信验证码
    public Map sendSms(String phone) {
        //1.生成验证码
        //String code = RandomUtil.randomNumbers(6);
        // TODO: 2021/7/3 验证码写死 以后需要改
        String code = "123456";
        //2.存入redis中    超时时间5分钟
        stringRedisTemplate.opsForValue().set(ConstantUtil.SMS_CODE + phone, code, Duration.ofMinutes(5));
        return null;

    }
    //修改手机号时，对用户输入的验证码进行校验
    public Map<String, Boolean> verifyCode(String verificationCode) {
        String phone = UserHolder.get().getPhone();
        //1.获取redis中的验证码
        String redisCode = stringRedisTemplate.opsForValue().get(ConstantUtil.SMS_CODE + phone);
        // 2.进行校验
        Map<String, Boolean> map = new HashMap<>();
        Boolean flag = true;
        //  不相等
        if (!StrUtil.equals(redisCode, verificationCode)) {
            flag = false;
        }
            //相等
        map.put("verification", flag);
        //当操作完code之后    要删除缓存的验证码
        stringRedisTemplate.delete(ConstantUtil.SMS_CODE + phone);
        System.out.println("------" + map.get("verification"));
        return map;
    }

    //修改手机号，保存修改后的手机号
    @Reference
    private UserService userService;
    public void updatePhoneByUserId(User user,String phone) {
        userService.updateUserId(user,phone);
    }
}
